package com.lws.basic.utils.common;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;

import java.util.ArrayList;

/**
 * 检查权限,如果未授权自动申请的工具类
 */

public class CheckPermissionUtil {
    public static void checkPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= 23) {
            int accessFineLocation = activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);//读取精确信息
            int camera = activity.checkSelfPermission(Manifest.permission.CAMERA);//访问
            int writeSD = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int readPhoneState = activity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
            //android.permission.READ_PHONE_STATE
            int requestCode = 0;
            ArrayList<String> permissions = new ArrayList<String>();
            if (accessFineLocation != PackageManager.PERMISSION_GRANTED) {
                requestCode |= 1 << 0;
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (camera != PackageManager.PERMISSION_GRANTED) {
                requestCode |= 1 << 1;
                permissions.add(Manifest.permission.CAMERA);
            }
            if (writeSD != PackageManager.PERMISSION_GRANTED) {
                requestCode |= 1 << 2;
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (readPhoneState != PackageManager.PERMISSION_GRANTED) {
                requestCode |= 1 << 3;
                permissions.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (requestCode > 0) {
                String[] permission = new String[permissions.size()];
                activity.requestPermissions(permissions.toArray(permission), requestCode);
                return;
            }
        }
    }

    public static boolean CheckGps(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
}
